//
//  NewfeatureViewController.swift
//  weibo1990
//
//  Created by 曹晓龙 on 15/10/19.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit

/// 重用标示
private let NewfeatureViewreuseIdentifier = "NewfeatureViewreuseIdentifier"

/// 图片张数
private let NewfeatureImageCount = 4

/// 新特性界面
class NewfeatureViewController: UICollectionViewController {

    //MARK: -构造方法
    init() {
        
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        layout.scrollDirection = .Horizontal
        
        layout.minimumInteritemSpacing = 0
        
        layout.minimumLineSpacing = 0
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.showsHorizontalScrollIndicator = false
        
        collectionView?.pagingEnabled = true
        
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = UIColor.lightGrayColor()
        
        self.collectionView!.registerClass(NewfeatureCell.self, forCellWithReuseIdentifier: NewfeatureViewreuseIdentifier)

    }

    // MARK: - 数据源方法

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return NewfeatureImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NewfeatureViewreuseIdentifier, forIndexPath: indexPath) as! NewfeatureCell
        
        cell.imageIndex = indexPath.item
 
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width )
        
        if page == NewfeatureImageCount - 1 {
            
            let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewfeatureCell
            
            cell.startAnim()
        }
    }
}

/// 自定义cell
class NewfeatureCell :UICollectionViewCell {
    
    
    ///  按钮点击事件
    @objc private func clickStartBtn(){

        NSNotificationCenter.defaultCenter().postNotificationName(WBSwiftNotificationKey, object: nil)

    }
    
    /// 传入cell 的序号
    private var imageIndex:Int = 0{
        
        didSet{
            
            imgView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            
            startBtn.hidden = true
        }
        
    }
    
    ///  立即体验按钮动画效果
    func startAnim(){

        startBtn.hidden = false
        
        startBtn.userInteractionEnabled = false
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration( 1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
                self.startBtn.userInteractionEnabled = true
        }
    }
    
    //MARK: - 构造方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }

    ///  设置UI界面
    private func setupUI(){
        
        //添加控键
        contentView.addSubview(imgView)
        contentView.addSubview(startBtn)
        
        //添加点击事件
        startBtn.addTarget(self, action: "clickStartBtn", forControlEvents: .TouchUpInside)
        // 自动布局
        // 背景图片
        imgView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            make.bottom.equalTo(contentView.snp_bottom)
        }
        
        // 立即体验按钮
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 背景图片
    lazy var imgView:UIImageView = UIImageView()
    lazy var startBtn:UIButton = UIButton(title: "立即体验", backImageName: "new_feature_finish_button", titleColor: UIColor.whiteColor())
}


